package com.juku2024.juku.widget

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.juku2024.juku.R
import android.animation.ObjectAnimator
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.Button
import android.widget.TextView
import com.juku2024.juku.tools.AppUtils


class VideoDesView(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {

    private val tvInfoTitle: TextView
    private val tvInfo1: TextView
    private val tvInfo2: TextView
    private val tvDes: TextView
    private val btnBack: Button

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_video_des, this, true)
        val color = AppUtils.getColor(R.color.app_window_background)
        view.setBackgroundColor(color)

        tvInfoTitle = view.findViewById(R.id.tv_info_title)
        tvInfo1 = view.findViewById(R.id.tv_info1)
        tvInfo2 = view.findViewById(R.id.tv_info2)
        tvDes = view.findViewById(R.id.tv_des)
        btnBack = view.findViewById(R.id.btnBack)

    }

    private var title: String? = null
    private var director: String? = null
    private var actor: String? = null
    private var description: String? = null
    private var isInit = false

    fun setDesInfo(title: String?, director: String?, actor: String?, description: String?) {
        this.title = title
        this.director = director
        this.actor = actor
        this.description = description

        if (isInit) {
            val directorSpan = "导演：$director"
            val actorSpan = "演员：$actor"
            val descriptionSpan = "简介：$description"

            tvInfoTitle.text = title
            tvInfo1.text = getColorSpan(directorSpan, directorSpan.length)
            tvInfo2.text = getColorSpan(actorSpan, actorSpan.length)
            tvDes.text = getColorSpan(descriptionSpan, descriptionSpan.length)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        isInit = true
        btnBack.setOnClickListener {
            hide()
        }
        val directorSpan = "导演：$director"
        val actorSpan = "演员：$actor"
        val descriptionSpan = "简介：$description"

        tvInfoTitle.text = title
        tvInfo1.text = getColorSpan(directorSpan, directorSpan.length)
        tvInfo2.text = getColorSpan(actorSpan, actorSpan.length)
        tvDes.text = getColorSpan(descriptionSpan, descriptionSpan.length)
    }

    @Suppress("DEPRECATION")
    private fun getColorSpan(des: String?, end: Int): SpannableString? {
        if (TextUtils.isEmpty(des)) return null
        val spanString = SpannableString(des)
        if (3 > end) {
            return spanString
        }
        val span = ForegroundColorSpan(context.resources?.getColor(R.color.color_text_dark) ?: 0)
        spanString.setSpan(span, 3, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        return spanString
    }

    private var isShowingDes = false
    fun show() {
        isShowingDes = true
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 2000f, 0f)
        animator.duration = 200
        animator.interpolator = DecelerateInterpolator()
        animator.start()
    }

    fun isShowing(): Boolean {
        return isShowingDes
    }

    fun hide() {
        if (isShowingDes) {
            isShowingDes = false
            val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 0f, 2000f)
            animator.duration = 200
            animator.interpolator = AccelerateInterpolator()
            animator.start()
        }
    }
}